from rest_framework import generics
from rest_framework.views import APIView
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi
from .models import Subject
from .serializers import SubjectSerializer, SubjectCreateSerializer

class SubjectListView(generics.ListAPIView):
    queryset = Subject.objects.filter(parent__isnull=True)
    serializer_class = SubjectSerializer
    # 使用 swagger_auto_schema 补充文档描述
    @swagger_auto_schema(
        operation_summary="获取学科以及章节列表（类似于菜单）",
        operation_description="返回父级为空的学科列表，支持按名称过滤",
        responses={
            200: openapi.Response("成功响应", SubjectSerializer(many=True)),
            400: "参数错误"
        }
    )
    def get(self, request, *args, **kwargs):
        return super().get(request, *args, **kwargs)



class SubjectCreateView(generics.CreateAPIView):
    queryset = Subject.objects.all()
    serializer_class = SubjectCreateSerializer
    
    @swagger_auto_schema(
        operation_summary="创建学科/章节",
        operation_description="创建新的学科或章节，支持设置父级学科",
        request_body=SubjectCreateSerializer,
        responses={
            201: openapi.Response(
                description="创建成功",
                schema=SubjectCreateSerializer
            ),
            400: openapi.Response(description="参数错误")
        }
    )
    def post(self, request, *args, **kwargs):
        return super().post(request, *args, **kwargs)